package com.skyable.account.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.skyable.account.entity.SysCompany;
import com.skyable.account.service.ICompanyService;
import com.skyable.common.common.ResponseResult;
import com.skyable.common.common.ServiceResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author GaoYuan
 * @title: CompanyController
 * @projectName private-cloud
 * @description: TODO
 * @date 2021/6/2921:38
 */
@Api(value = "租户管理")
@RestController
@RequestMapping("company")
public class CompanyController extends BaseController {

    @Autowired
    private ICompanyService sysCompanyService;

    /**
     * 新增租户信息
     * @param sysCompany
     * @return
     */
    @ApiOperation(value = "新增租户信息")
    @PostMapping("/insert")
    public ResponseResult insertCompany(@RequestBody SysCompany sysCompany, HttpServletRequest request) {
        setInsertInfo(sysCompany,request);
        ServiceResult<SysCompany> serviceResult = sysCompanyService.insertCompany(sysCompany);
        if(!serviceResult.getSuccess()) {
            return ResponseResult.error(serviceResult.getCode(),serviceResult.getMessage());
        }

        return ResponseResult.success(serviceResult.getResult());
    }

    /**
     * 更新租户信息
     * @param sysCompany
     * @return
     */
    @ApiOperation(value = "更新租户信息")
    @PutMapping("/update")
    public ResponseResult updateCompany(@RequestBody SysCompany sysCompany, HttpServletRequest request) {
        setUpdateInfo(sysCompany,request);
        ServiceResult<SysCompany> serviceResult = sysCompanyService.updateCompany(sysCompany);
        if(!serviceResult.getSuccess()) {
            return ResponseResult.error(serviceResult.getCode(),serviceResult.getMessage());
        }

        return ResponseResult.success(serviceResult.getResult());
    }

    /**
     * 获取租户信息列表
     * @return
     */
    @ApiOperation(value = "获取租户信息列表")
    @GetMapping("/view")
    public ResponseResult sysCompanyList(){
        ServiceResult<List<SysCompany>> serviceResult = sysCompanyService.findCompanyList();
        if(!serviceResult.getSuccess()) {
            return ResponseResult.error(serviceResult.getCode(),serviceResult.getMessage());
        }
        return ResponseResult.success(serviceResult.getResult());
    }

    /**
     * 根据ID查询租户信息
     * @param deptId
     * @return
     */
    @ApiOperation(value = "根据ID查询租户信息")
    @GetMapping("/view/{id}")
    public ResponseResult getCompanyById(@PathVariable("id") Long deptId){
        ServiceResult<SysCompany> serviceResult = sysCompanyService.findCompanyById(deptId);
        if(!serviceResult.getSuccess()) {
            return ResponseResult.error(serviceResult.getCode(),serviceResult.getMessage());
        }
        return ResponseResult.success(serviceResult.getResult());
    }

    /**
     * 分页查询租户列表
     * @param page
     * @param limit
     * @param sysCompany
     * @return
     */
    @ApiOperation(value = "分页查询租户列表")
    @PostMapping("/view/{page}/{limit}")
    public ResponseResult sysCompanyList(@PathVariable("page")Integer page, @PathVariable("limit")Integer limit, @RequestBody SysCompany sysCompany){
        ServiceResult<IPage<SysCompany>> serviceResult = sysCompanyService.findCompanyListByPage(page,limit,sysCompany);
        if(!serviceResult.getSuccess()) {
            return ResponseResult.error(serviceResult.getCode(),serviceResult.getMessage());
        }
        return ResponseResult.success(serviceResult.getResult().getRecords());
    }

    /**
     * 删除租户信息
     * @param deptId
     * @return
     */
    @ApiOperation(value = "删除租户信息")
    @DeleteMapping("/delete/{id}")
    public ResponseResult deletePermission(@PathVariable("id") Long deptId){
        ServiceResult<Integer> serviceResult = sysCompanyService.deleteCompanyById(deptId);
        if(!serviceResult.getSuccess()) {
            return ResponseResult.error(serviceResult.getCode(),serviceResult.getMessage());
        }
        return ResponseResult.success(serviceResult.getResult());
    }
    
}
